package com.binc.testspring.controller;

import com.binc.testspring.dao.StudentMapper;
import com.binc.testspring.dto.Result;
import com.binc.testspring.dto.Student;
import com.binc.testspring.vo.IdVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 添加数据库链接
 *
 */
@Slf4j
@RestController
@RequestMapping("/mysql")
@Api(tags = "mysql测试")
public class Mysql_controller {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @ResponseBody
    @PostMapping("/doQuery")
    @ApiOperation(value = "查库", notes = "doQuery")
    public Map<String, Object> map() {
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from student");
        for (Map<String, Object> map : maps
        ) {
            System.out.println(map);
        }
        return maps.get(0);
    }

    @Autowired
    StudentMapper studentMapper;

    @ResponseBody
    @PostMapping("/allList")
    @ApiOperation(value = "allList", notes = "allList")
    public Result<List<Student>> allList(@RequestBody IdVo idVo) {
        List<Student> students = studentMapper.allList();
        return Result.of("ok",students);
    }

    @ResponseBody
    @PostMapping("/allList2")
    @ApiOperation(value = "allList2  这个走xml", notes = "allList2")
    public Result<List<Student>> allList2(@RequestBody IdVo idVo) {
        List<Student> students = studentMapper.allList2();
        return Result.of("ok",students);
    }

    @ResponseBody
    @PostMapping("/doQuery_by_id")
    @ApiOperation(value = "查库_by_id", notes = "doQuery_by_id")
    public Result<Student> doQuery_by_id(@RequestBody IdVo idVo) {
        Student student = studentMapper.queryUserById(idVo.getId());
        return Result.of("ok",student);
    }

    @ResponseBody
    @PutMapping("/insertStudent")
    @ApiOperation(value = "插入Student", notes = "insertStudent")
    public Result<Integer> insertStudent() {
        Long leastSignificantBits = UUID.randomUUID().getLeastSignificantBits();
        String name ="binc"+leastSignificantBits;
        int insert = studentMapper.insert(Student.builder().id(leastSignificantBits).name(name).age(15).build());
        return Result.of("ok",insert);
    }


}
